import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: "",
    userInfo: JSON.parse(sessionStorage.getItem("userInfo")),
    username: ''
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
      localStorage.setItem("token", token); //存储token，将token住区的信息存储
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo;
      sessionStorage.setItem("userInfo", JSON.stringify(userInfo)); //存储用户信息
    },
    REMOVE_INFO: (state) => {
      localStorage.setItem("token", "");
      sessionStorage.setItem("userInfo", JSON.stringify(""));
      state.userInfo = {};
    },
  },
  getters: {
    getUser: (state) => {
      return state.userInfo;
    },
  },
  actions: {},
  modules: {},
});
